#****************************************************************************************************
# File Start!
#***************************************************************************************************/

#*
 #
 #  Copyright (c) 2024-2025 by flechazo. All rights reserved.
 #
 # Author : CarlChai LinFeng Chai flechazo
 # Website: flechazo.mba
 #
 # Change Logs:
 # Date           Author       Notes
 # 2025-06-14     cc          the first version
 #
#/

#****************************************************************************************************
# Include
#***************************************************************************************************/

import subprocess
import pyautogui
import win32gui
import win32con
import keyboard

#****************************************************************************************************
# Define
#***************************************************************************************************/
MOUSE_SMALL_MOVE = 10
MOUSE_MEDIUM_MOVE = 50
MOUSE_LARGE_MOVE = 100

#****************************************************************************************************
# Type Define
#***************************************************************************************************/

#****************************************************************************************************
# Global Variable
#***************************************************************************************************/

#****************************************************************************************************
# Function Impletemelnt
#***************************************************************************************************/

#****************************************************************************************************
# command_cfg_execute_action()
#***************************************************************************************************/
def command_cfg_execute_action(action):
    # get database #
    from database.database import database_actions_get
    actions = database_actions_get()
    # find action #
    for category in actions.values():
        for item in category:
            # check #
            if item["name"] != action:
                continue
            if item["func"] == None:
                continue
            # execute #
            item["func"](action)
            return
    return

#****************************************************************************************************
# command_cfg_execute_action_OpenTaskManager()
#***************************************************************************************************/
def command_cfg_execute_action_OpenTaskManager(action):
    subprocess.Popen("start taskmgr.exe", shell=True)

#****************************************************************************************************
# command_cfg_execute_action_OpenControlPanel()
#***************************************************************************************************/
def command_cfg_execute_action_OpenControlPanel(action):
    subprocess.Popen("start control.exe", shell=True)

#****************************************************************************************************
# command_cfg_execute_action_OpenRun()
#***************************************************************************************************/
def command_cfg_execute_action_OpenRun(action):
    subprocess.Popen("start cmd.exe", shell=True)

#****************************************************************************************************
# command_cfg_execute_action_OpenExplorer()
#***************************************************************************************************/
def command_cfg_execute_action_OpenExplorer(action):
    subprocess.Popen("start explorer.exe", shell=True)

#****************************************************************************************************
# command_cfg_execute_action_OpenCalculator()
#***************************************************************************************************/
def command_cfg_execute_action_OpenCalculator(action):
    subprocess.Popen("start calc.exe", shell=True)

#****************************************************************************************************
# command_cfg_execute_action_OpenNotepad()
#***************************************************************************************************/
def command_cfg_execute_action_OpenNotepad(action):
    subprocess.Popen("start notepad.exe", shell=True)

#****************************************************************************************************
# command_cfg_execute_action_OpenPaint()
#***************************************************************************************************/
def command_cfg_execute_action_OpenPaint(action):
    subprocess.Popen("start mspaint.exe", shell=True)

#****************************************************************************************************
# command_cfg_execute_action_OpenCMD()
#***************************************************************************************************/
def command_cfg_execute_action_OpenCMD(action):
    subprocess.Popen("start cmd.exe", shell=True)

#****************************************************************************************************
# command_cfg_execute_action_OpenPowerShell()
#***************************************************************************************************/
def command_cfg_execute_action_OpenPowerShell(action):
    subprocess.Popen("start powershell.exe", shell=True)

#****************************************************************************************************
# command_cfg_execute_action_MinimizeWindow()
#***************************************************************************************************/
def command_cfg_execute_action_MinimizeWindow(action):
    hwnd = win32gui.GetForegroundWindow()
    win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE)

#****************************************************************************************************
# command_cfg_execute_action_MaximizeWindow()
#***************************************************************************************************/
def command_cfg_execute_action_MaximizeWindow(action):
    hwnd = win32gui.GetForegroundWindow()
    win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)

#****************************************************************************************************
# command_cfg_execute_action_CloseWindow()
#***************************************************************************************************/
def command_cfg_execute_action_CloseWindow(action):
    hwnd = win32gui.GetForegroundWindow()
    win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)

#****************************************************************************************************
# command_cfg_execute_action_AltTab()
#***************************************************************************************************/
def command_cfg_execute_action_AltTab(action):
    keyboard.press_and_release('alt+tab')

#****************************************************************************************************
# command_cfg_execute_action_OpenChrome()
#***************************************************************************************************/
def command_cfg_execute_action_OpenChrome(action):
    subprocess.Popen("start chrome.exe", shell=True)

#****************************************************************************************************
# command_cfg_execute_action_OpenDefaultBrowser()
#***************************************************************************************************/
def command_cfg_execute_action_OpenDefaultBrowser(action):
    subprocess.Popen("start https://www.google.com", shell=True)

#****************************************************************************************************
# command_cfg_execute_action_ShowDesktop()
#***************************************************************************************************/
def command_cfg_execute_action_ShowDesktop(action):
    keyboard.press_and_release('win+d')

#****************************************************************************************************
# command_cfg_execute_action_LockComputer()
#***************************************************************************************************/
def command_cfg_execute_action_LockComputer(action):
    keyboard.press_and_release('win+l')


#****************************************************************************************************
# command_cfg_execute_action_SwitchInput()
#***************************************************************************************************/
def command_cfg_execute_action_SwitchInput(action):
    keyboard.press_and_release('alt+shift')

#****************************************************************************************************
# command_cfg_execute_action_Screenshot()
#***************************************************************************************************/
def command_cfg_execute_action_Screenshot(action):
    keyboard.press_and_release('win+shift+s')

#****************************************************************************************************
# command_cfg_execute_action_VolumeMute()
#***************************************************************************************************/
def command_cfg_execute_action_VolumeMute(action):
    keyboard.press_and_release('volume mute')

#****************************************************************************************************
# command_cfg_execute_action_VolumeUp()
#***************************************************************************************************/
def command_cfg_execute_action_VolumeUp(action):
    keyboard.press_and_release('volume up')

#****************************************************************************************************
# command_cfg_execute_action_VolumeDown()
#***************************************************************************************************/
def command_cfg_execute_action_VolumeDown(action):
    keyboard.press_and_release('volume down')

#****************************************************************************************************
# command_cfg_execute_action_MouseUpSmall()
#***************************************************************************************************/
def command_cfg_execute_action_MouseUpSmall(action):
    pyautogui.moveRel(0, -MOUSE_SMALL_MOVE)

#****************************************************************************************************
# command_cfg_execute_action_MouseDownSmall()
#***************************************************************************************************/
def command_cfg_execute_action_MouseDownSmall(action):
    pyautogui.moveRel(0, MOUSE_SMALL_MOVE)

#****************************************************************************************************
# command_cfg_execute_action_MouseLeftSmall()
#***************************************************************************************************/
def command_cfg_execute_action_MouseLeftSmall(action):
    pyautogui.moveRel(-MOUSE_SMALL_MOVE, 0)

#****************************************************************************************************
# command_cfg_execute_action_MouseRightSmall()
#***************************************************************************************************/
def command_cfg_execute_action_MouseRightSmall(action):
    pyautogui.moveRel(MOUSE_SMALL_MOVE, 0)

#****************************************************************************************************
# command_cfg_execute_action_MouseUpMedium()
#***************************************************************************************************/
def command_cfg_execute_action_MouseUpMedium(action):
    pyautogui.moveRel(0, -MOUSE_MEDIUM_MOVE)

#****************************************************************************************************
# command_cfg_execute_action_MouseDownMedium()
#***************************************************************************************************/
def command_cfg_execute_action_MouseDownMedium(action):
    pyautogui.moveRel(0, MOUSE_MEDIUM_MOVE)

#****************************************************************************************************
# command_cfg_execute_action_MouseLeftMedium()
#***************************************************************************************************/
def command_cfg_execute_action_MouseLeftMedium(action):
    pyautogui.moveRel(-MOUSE_MEDIUM_MOVE, 0)

#****************************************************************************************************
# command_cfg_execute_action_MouseRightMedium()
#***************************************************************************************************/
def command_cfg_execute_action_MouseRightMedium(action):
    pyautogui.moveRel(MOUSE_MEDIUM_MOVE, 0)

#****************************************************************************************************
# command_cfg_execute_action_MouseUpLarge()
#***************************************************************************************************/
def command_cfg_execute_action_MouseUpLarge(action):
    pyautogui.moveRel(0, -MOUSE_LARGE_MOVE)

#****************************************************************************************************
# command_cfg_execute_action_MouseDownLarge()
#***************************************************************************************************/
def command_cfg_execute_action_MouseDownLarge(action):
    pyautogui.moveRel(0, MOUSE_LARGE_MOVE)

#****************************************************************************************************
# command_cfg_execute_action_MouseLeftLarge()
#***************************************************************************************************/
def command_cfg_execute_action_MouseLeftLarge(action):
    pyautogui.moveRel(-MOUSE_LARGE_MOVE, 0)

#****************************************************************************************************
# command_cfg_execute_action_MouseRightLarge()
#***************************************************************************************************/
def command_cfg_execute_action_MouseRightLarge(action):
    pyautogui.moveRel(MOUSE_LARGE_MOVE, 0)

#****************************************************************************************************
# command_cfg_execute_action_MouseDragStart()
#***************************************************************************************************/
def command_cfg_execute_action_MouseDragStart(action):
    pyautogui.mouseDown()

#****************************************************************************************************
# command_cfg_execute_action_MouseDragEnd()
#***************************************************************************************************/
def command_cfg_execute_action_MouseDragEnd(action):
    pyautogui.mouseUp()

#****************************************************************************************************
# command_cfg_execute_action_MouseRightDragStart()
#***************************************************************************************************/
def command_cfg_execute_action_MouseRightDragStart(action):
    pyautogui.mouseDown(button='right')

#****************************************************************************************************
# command_cfg_execute_action_MouseRightDragEnd()
#***************************************************************************************************/
def command_cfg_execute_action_MouseRightDragEnd(action):
    pyautogui.mouseUp(button='right')

#****************************************************************************************************
# command_cfg_execute_action_MouseWheelUp()
#***************************************************************************************************/
def command_cfg_execute_action_MouseWheelUp(action):
    pyautogui.scroll(1)

#****************************************************************************************************
# command_cfg_execute_action_MouseWheelDown()
#***************************************************************************************************/
def command_cfg_execute_action_MouseWheelDown(action):
    pyautogui.scroll(-1)

#****************************************************************************************************
# command_cfg_execute_action_MouseWheelUpLarge()
#***************************************************************************************************/
def command_cfg_execute_action_MouseWheelUpLarge(action):
    pyautogui.scroll(5)

#****************************************************************************************************
# command_cfg_execute_action_MouseWheelDownLarge()
#***************************************************************************************************/
def command_cfg_execute_action_MouseWheelDownLarge(action):
    pyautogui.scroll(-5)

#****************************************************************************************************
# command_cfg_execute_action_MouseLeftClick()
#***************************************************************************************************/
def command_cfg_execute_action_MouseLeftClick(action):
    pyautogui.click()

#****************************************************************************************************
# command_cfg_execute_action_MouseRightClick()
#***************************************************************************************************/
def command_cfg_execute_action_MouseRightClick(action):
    pyautogui.rightClick()

#****************************************************************************************************
# command_cfg_execute_action_MouseDoubleClick()
#***************************************************************************************************/
def command_cfg_execute_action_MouseDoubleClick(action):
    pyautogui.doubleClick()

#****************************************************************************************************
# command_cfg_execute_action_MouseMiddleClick()
#***************************************************************************************************/
def command_cfg_execute_action_MouseMiddleClick(action):
    pyautogui.middleClick()

#****************************************************************************************************
# exports
#***************************************************************************************************/

#****************************************************************************************************
# File End!
#***************************************************************************************************/
